# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gtk
import pango

import InterfaceUtils
import EditCategoryDialog
import NewCategoryDialog

class CategoryView(gtk.Frame):
	def __init__(self, account, mainWindow):
		gtk.Frame.__init__(self)
		self.wTree = InterfaceUtils.GetGladeTree("CategoryViewHolder")
		
		signals = {
			"OnNewCategory"		: self.OnNewCategory,
			"OnDeleteCategory"	: self.OnDeleteCategory,
			"OnEditCategory"	: self.OnEditCategory,
			"OnTreeViewSelection"	: self.OnTreeViewSelection
		}
		
		self.wTree.signal_autoconnect(signals)
		
		self.vbox = self.wTree.get_widget("CategoryViewVBox")
		self.wTree.get_widget("CategoryViewHolder").remove(self.vbox)
		self.add(self.vbox)
		
		self.account = account
		self.account.connect("categories", self.UpdateInterface)
		self.account.connect("people", self.UpdateInterface)
		
		self.mainWindow = mainWindow
		self.treeview = self.wTree.get_widget("CategoriesTreeView")
		self.deleteButton = self.wTree.get_widget("DeleteCategoryButton")
		self.newButton = self.wTree.get_widget("NewCategoryButton")
		self.closeButton = self.wTree.get_widget("CloseButton")
		self.editButton = self.wTree.get_widget("EditCategoryButton")
		
		self.model = gtk.ListStore(str, str, str, pango.Style, pango.Style)
		self.treeview.set_model(self.model)
		
		self.treeview.insert_column_with_attributes(-1, "Name", gtk.CellRendererText(), text=0)
		self.treeview.insert_column_with_attributes(-1, "Paid By", gtk.CellRendererText(), text=1, style=3)
		self.treeview.insert_column_with_attributes(-1, "Shared By", gtk.CellRendererText(), text=2, style=4)
		
		#desensitize widgets
		self.OnTreeViewSelection()
		
		self.show_all()
	
	def OnNewCategory(self, widget=None):
		NewCategoryDialog.NewCategoryDialog(self.mainWindow, self.account, self.AddCategory)
		
	def AddCategory(self, name, paidBy, sharedBy):
		self.account.addCategory(name, paidBy, sharedBy)
	
	def OnEditCategory(self, widget):
		EditCategoryDialog.EditCategoryDialog(self.mainWindow, self.account, self.ChangeCategory, self.GetSelectedName())
		
	def ChangeCategory(self, oldName, newName, paidBy, sharedBy):
		self.account.changeCategory(oldName, newName, paidBy, sharedBy)
	
	def OnDeleteCategory(self, widget):
		name = self.GetSelectedName()
		if name:
			self.account.removeCategory(name)
		
	def OnTreeViewSelection(self, widget=None):
		selected = (self.treeview.get_cursor()[0] != None)
		for i in (self.deleteButton, self.editButton):
			i.set_sensitive(selected)
	
	def GetSelectedName(self):
		path = self.treeview.get_cursor()[0]
		if path:
			row = self.model[path][0]
			return row
		return None
	
	def UpdateModel(self):
		path = self.treeview.get_cursor()[0]
		
		self.model.clear()
		for category in self.account.getCategories():
			name = category.getName()
			paid = category.getPaidBy()
			shared = category.getSharedBy()
			paidStyle = pango.STYLE_NORMAL
			sharedStyle = pango.STYLE_NORMAL
			
			if not paid:
				paid = "Anyone"
				paidStyle = pango.STYLE_ITALIC
			if not shared:
				shared = "Anyone"
				sharedStyle = pango.STYLE_ITALIC
			else:
				shared = shared.getPrettyString()
			
			self.model.append((name, paid, shared, paidStyle, sharedStyle))
			
		if path and len(self.model):
			if path[0] >= len(self.model):
				index = len(self.model) - 1
			else:
				index = path[0]
			self.treeview.set_cursor( (index,) )
				
	def UpdateInterface(self, widget=None):
		self.UpdateModel()
